import { mount } from '@vue/test-utils'
import { createPinia } from 'pinia'
import { createRouter, createWebHistory } from 'vue-router'

// 创建测试用的 Pinia store
export function createTestPinia() {
  return createPinia()
}

// 创建测试用的路由
export function createTestRouter(routes = []) {
  return createRouter({
    history: createWebHistory(),
    routes
  })
}

// 创建带插件的组件包装器
export function createTestWrapper(component, options = {}) {
  const {
    plugins = [],
    props = {},
    slots = {},
    global = {}
  } = options

  return mount(component, {
    props,
    slots,
    global: {
      plugins,
      ...global
    }
  })
}

// 创建带路由的组件包装器
export function createRouterWrapper(component, routes = [], options = {}) {
  const router = createTestRouter(routes)
  const pinia = createTestPinia()
  
  return createTestWrapper(component, {
    ...options,
    plugins: [router, pinia, ...(options.plugins || [])]
  })
}

// 等待异步操作完成
export function waitForNextTick() {
  return new Promise(resolve => setTimeout(resolve, 0))
}

// 等待指定时间
export function waitFor(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

// 模拟用户事件
export async function simulateUserInput(wrapper, selector, value) {
  const input = wrapper.find(selector)
  await input.setValue(value)
  await input.trigger('input')
}

// 模拟点击事件
export async function simulateClick(wrapper, selector) {
  const element = wrapper.find(selector)
  await element.trigger('click')
}

// 模拟表单提交
export async function simulateFormSubmit(wrapper, selector) {
  const form = wrapper.find(selector)
  await form.trigger('submit')
}
